package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.activity.MainActivity;
import com.projet.whattowatch.adapter.MovieAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class SearchMovieFragment extends Fragment
{
	private EditText keyword;
	private Spinner critere;
	private Button searchButton;
	private Button advancedButton;
	private ListView searchList;
	
	final String EXTRA_MOVIE_NAME = "movie_name";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View rootView = inflater.inflate(R.layout.search_movie_layout, container, false);
		
		keyword = (EditText) rootView.findViewById(R.id.keywordEditText);
		critere = (Spinner) rootView.findViewById(R.id.critereSpinner);
		searchButton = (Button) rootView.findViewById(R.id.searchButton);
		advancedButton = (Button) rootView.findViewById(R.id.advancedSearchButton);
		searchList = (ListView) rootView.findViewById(R.id.searchListView);
		
		searchButton = (Button) rootView.findViewById(R.id.searchButton);
		searchButton.setOnClickListener(searchListener);
		advancedButton.setOnClickListener(advancedListener);
		
		return rootView;
	}
	
	public OnClickListener searchListener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
    		String stringKeyword = keyword.getText().toString();
    		String stringCritere = critere.getSelectedItem().toString();
    		
    		ArrayList<String> result = MainActivity.mLibFilm.search(stringKeyword, stringCritere);
    		ArrayList<String> movieName = new ArrayList<String>();
            ArrayList<String> movieUrl = new ArrayList<String>();
    		
    		for(int i = 0; i < result.size(); i++)
    		{
    			for(int j = 0; j < MainActivity.mLibFilm.getListFilm().size(); j++)
	            {
    				if(result.get(i).equals(MainActivity.mLibFilm.getListFilm().get(j).getNom()))
    				{
    	    			movieName.add(MainActivity.mLibFilm.getListFilm().get(j).getNom());
    	    			movieUrl.add(MainActivity.mLibFilm.getListFilm().get(j).getImageUrl());
    				}
	            }
    		}
    		
    		MovieAdapter adapter = new MovieAdapter(getActivity(), movieName, movieUrl);

    		searchList.setAdapter(adapter);
    		hideSoftKeyboard();
        }
    };
    
    public OnClickListener advancedListener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	Intent advancedSearch = new Intent(getActivity().getApplicationContext(), AdvancedSearchActivity.class);
        	startActivity(advancedSearch);
        }
    };
    
    private void hideSoftKeyboard()
    {
        if(getActivity().getCurrentFocus()!=null && getActivity().getCurrentFocus() instanceof EditText){
            InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getActivity().getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }
}